﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media;

namespace EnvControlLib
{
    /// <summary>
    /// 窗口基类
    /// </summary>
    public class EnvBaseWindow : Window
    {
        #region Private Fields

        private ResourceDictionary resourceDictionary;
        private bool closeStoryBoardCompleted = false;
        private DoubleAnimation closeAnimation1, closeAnimation2;
        private Rect rectNormal; // 定义一个全局 rect 记录还原状态下窗口的位置和大小

        #endregion

        #region Constructor

        public EnvBaseWindow()
        {
            resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("EnvControlLib;component/Themes/EnvWindowStyleGroup.xaml",
                UriKind.Relative);

            InitializeStyle();
            this.Loaded += delegate
            {
                InitializeEvent();
            };
            this.Closing += new System.ComponentModel.CancelEventHandler(EnvBaseWindow_Closing);
        }

        #endregion

        #region Private Methods

        private void InitializeStyle()
        {
            this.Style = (System.Windows.Style)resourceDictionary["BaseWindowStyle"];
        }

        private void InitializeEvent()
        {
            ControlTemplate envBaseWindowTemplate = (ControlTemplate)resourceDictionary["BaseWindowControlTemplate"];

            Button minBtn = (Button)envBaseWindowTemplate.FindName("btnMin", this);
            minBtn.Click += delegate
            {
                this.WindowState = WindowState.Minimized;
            };

            Button maxBtn = (Button)envBaseWindowTemplate.FindName("btnMax", this);
            maxBtn.Click += delegate
            {
                //this.WindowState = (this.WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal);
                Rect rectangle = SystemParameters.WorkArea; // 获取工作区大小
                if (this.Width == rectangle.Width) // 一般窗口
                {
                    this.Left = rectNormal.Left == 0 ? 200 : rectNormal.Left;
                    this.Top = rectNormal.Top == 0 ? 100 :rectangle.Top;
                    this.Width = rectNormal.Width == 0 ? 640 : rectNormal.Width;
                    this.Height = rectNormal.Height == 0 ? 480 : rectNormal.Height;
                    this.WindowState = WindowState.Normal;
                }
                else // 最大化
                {
                    rectNormal = new Rect(this.Left, this.Top, this.Width, this.Height); // 保存当前位置与大小
                    this.Left = 0;
                    this.Top = 0;
                    this.Width = rectangle.Width;
                    this.Height = rectangle.Height;
                }
            };

            Button closeBtn = (Button)envBaseWindowTemplate.FindName("btnClose", this);
            closeBtn.Click += delegate
            {
                this.Close();
            };

            Border borderTitle = (Border)envBaseWindowTemplate.FindName("borderTitle", this);
            borderTitle.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            };
            borderTitle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
            {
                if (e.ClickCount >= 2) // 双击标题栏
                {
                    maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                }
            };
        }

        #endregion

        #region Events

        private void EnvBaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!closeStoryBoardCompleted)
            {
                closeAnimation1 = new DoubleAnimation();
                closeAnimation1.From = 1;
                closeAnimation1.To = 0.1;
                closeAnimation1.Duration = new Duration(TimeSpan.Parse("0:0:0.3"));
                closeAnimation2 = new DoubleAnimation();
                closeAnimation2.From = 1;
                closeAnimation2.To = 0;
                closeAnimation2.Duration = new Duration(TimeSpan.Parse("0:0:0.3"));
                closeAnimation2.BeginTime = TimeSpan.Parse("0:0:0.4");
                closeAnimation2.Completed += new EventHandler(closeWindow_Completed);
                ScaleTransform st = new ScaleTransform();
                st.CenterX = this.Width / 2;
                st.CenterY = this.Height / 2;
                this.RenderTransform = st;
                st.BeginAnimation(ScaleTransform.ScaleYProperty, closeAnimation1);
                st.BeginAnimation(ScaleTransform.ScaleXProperty, closeAnimation2);
                e.Cancel = true;
            }
        }

        private void closeWindow_Completed(object sender, EventArgs e)
        {
            closeStoryBoardCompleted = true;
            this.Close();
        }

        #endregion
    }
}
